﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.Attributes;
using CoreDB.Util;
using CoreDB.Extensions;
using Autodesk.Revit.UI.Selection;

namespace CoreDB
{
    [Transaction(TransactionMode.Manual)]
    [Regeneration(RegenerationOption.Manual)]
    [Journaling(JournalingMode.NoCommandData)]
    public class Command : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIApplication uiapp = commandData.Application;
            UIDocument uidoc = uiapp.ActiveUIDocument;
            Document doc = uidoc.Document;

            var mc = doc.GetElement(uidoc.Selection.PickObject(ObjectType.Element, "gef")) as ModelCurve;
            var mcs = uidoc.Selection.PickObjects(ObjectType.Element, "ge").Select(x => doc.GetElement(x) as ModelCurve).ToList();

            //Element selElem = doc.GetElement(uidoc.Selection.PickObject(ObjectType.Element, "fe"));
            using (TransactionWrapper t = new TransactionWrapper(doc, "ge"))
            {
                //Line originLine = (doc.GetElement(new ElementId(563348)) as ModelCurve).GeometryCurve as Line;
                var ls = mcs.Select(x => x.GeometryCurve as Line).ToList();
                var l = mc.GeometryCurve as Line;
                var results = l.ProjectUnion(ls);
               

                results.Flat(20).Draw(doc, true);
                t.Commit();
            }



            return Result.Succeeded;
        }
    }
}
